import server from '@/utils/https.ts'
import type { UnifyResponse } from '@/types/result'
import type {
  ArtistVO,
  CarouselDTO,
  GenreDTO,
  PageDTO,
  PlayListItem,
  RecommendSongs,
  SongsPageDTO,
  SongsPageInfo,
  SongsVO,
} from '@/types/song'

const URL = 'http://localhost:8888'

export const getGeneAPI = async (): Promise<UnifyResponse<GenreDTO[]>> => {
  const response = await server.get('/api/songs/genres')
  return response.data
}

export const postSongAPI = async (formData: FormData): Promise<UnifyResponse<string>> => {
  const response = await server.post(URL + '/songs/uploadSongs', formData)
  return response.data
}

export const getArtistListAPI = async (input?: string): Promise<UnifyResponse<ArtistVO[]>> => {
  const params = new URLSearchParams()
  if (input !== undefined) {
    params.append('input', input)
  }
  const response = await server.get(`/api/artist?${params.toString()}`)
  return response.data
}

export const getCarouselListAPI = async (): Promise<UnifyResponse<CarouselDTO[]>> => {
  const response = await server.get('/api/songs/carousels')
  return response.data
}

export const getSongsListAPI = async (data: SongsPageInfo): Promise<UnifyResponse<PageDTO<SongsPageDTO[]>>> => {
  const params = new URLSearchParams({
    page: data.page.toString(),
    size: data.size.toString(),
    sortBy: data.sortBy,
    sortOrder: data.sortOrder,
  })
  if (data.genreId !== undefined) {
    params.append('genreId', data.genreId.toString())
  }
  const response = await server.get(`/api/songs/songsPage?${params.toString()}`)
  return response.data
}

export const getSongAPI = async (id: number): Promise<UnifyResponse<SongsVO>> => {
  const response = await server.get(`/api/songs/song?id=${id}`)
  return response.data
}

export const postSongToPlayListAPI = async (id: number): Promise<UnifyResponse<String>> => {
  const response = await server.post(`/api/songs/insertSongToPlayList`, {
    id: id,
  })
  return response.data
}

export const postPlayListAPI = async (): Promise<UnifyResponse<PlayListItem[]>> => {
  const response = await server.post(`/api/songs/playList`)
  return response.data
}

export const postLoveSongAPI = async (id: number) => {
  const response = await server.post(`/api/songs/love`, {
    id: id,
  })
  return response.data
}

export const getIsLoveSongAPI = async (id: number) => {
  const response = await server.get(`/api/songs/love?id=${id}`)
  return response.data
}

export const getRecommendSongAPI = async (songId: number): Promise<UnifyResponse<RecommendSongs[]>> => {
  const response = await server.get(`/api/songs/recommend?songId=${songId}`)
  return response.data
}

export const getUserFavoritesAPI = async (data: SongsPageInfo): Promise<UnifyResponse<PageDTO<SongsPageDTO[]>>> => {
  const params = new URLSearchParams({
    page: data.page.toString(),
    size: data.size.toString(),
    sortBy: data.sortBy,
    sortOrder: data.sortOrder,
  })
  const response = await server.get(`/api/songs/favorite?${params.toString()}`)
  return response.data
}
